#include "mobile_rc_server.hpp"

#include <FS.h>
#include <LittleFS.h>  // 如果使用 SPIFFS，请改为 #include <SPIFFS.h>
#include <WiFi.h>

#include "communicate.hpp"
#include "htmls.hpp"
#include "motion_control.hpp"

// Includes for the server
// Note: We include HTTPServer and HTTPSServer
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>
#include <HTTPSServer.hpp>
#include <HTTPServer.hpp>
#include <SSLCert.hpp>

// Easier access to the classes of the server
using namespace httpsserver;

#define CRT \
    "-----BEGIN CERTIFICATE-----\n\
MIID4zCCAsugAwIBAgIUWfH+2xmhZzxO2FI0KnLr8PiZCm4wDQYJKoZIhvcNAQEL\n\
BQAwgYAxCzAJBgNVBAYTAkNOMRIwEAYDVQQIDAlHdWFuZ2RvbmcxETAPBgNVBAcM\n\
CFNoZW56aGVuMQ0wCwYDVQQKDARTTUJVMQ0wCwYDVQQLDARTTUJVMQ0wCwYDVQQD\n\
DARTTUJVMR0wGwYJKoZIhvcNAQkBFg4xMUBzbWJ1LmVkdS5jbjAeFw0yNTA0MTUw\n\
ODMyMjBaFw0yNjA0MTUwODMyMjBaMIGAMQswCQYDVQQGEwJDTjESMBAGA1UECAwJ\n\
R3Vhbmdkb25nMREwDwYDVQQHDAhTaGVuemhlbjENMAsGA1UECgwEU01CVTENMAsG\n\
A1UECwwEU01CVTENMAsGA1UEAwwEU01CVTEdMBsGCSqGSIb3DQEJARYOMTFAc21i\n\
dS5lZHUuY24wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQD0SwlO6wgR\n\
TUs52/F5TbG2kYnqUJwj5FPFOlKcd7qguAzjRhdZNWeSP9Kgm3ARDU84ipYI53AU\n\
sSGEoBojoJv+7a74XKxx2b7uiYAHALIggEDq4r/0iI4uRA9UfXxwvzDqwQp/Pgx4\n\
j3e+ext0noc3ljS8dA4oneHx0D6UaFKTRmrnjrQeCWcsmmbh6h8FJRTlL5+V87+M\n\
ahP//3M9E6MEFAU2gS7WvTygsWAIU6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Gkt5UeMjn3EVlpwahyocQLGp6Mu7YpmODm2oQE63gMF\n\
pV8yf6vdQyElRkju46FQWF8izIX59HSLOdKgpcZlAoGARKQfGtL6Tsw1ATOnXOND\n\
sN57SEoUV/X3PqwG3WmP8etM63mIZsJHlSNdzAFxpix6RG8KXfvaLR1cZLzhP8ji\n\
Ayvqf2F4PHqiMKLNenAD1SLpziLC0geybgjB0jEGNtgMRCaukhejNM9PaxD/SHS4\n\
dFRNL3axbouuxXDgswzXJ/0=\n\
-----END PRIVATE KEY-----"

namespace mobile_remote_control_server
{
const char * ssid = "Mobile_RC_AP";
const char * password = "";

const char * crt = CRT;
const char * key = KEY;
unsigned char * crt_DER = (unsigned char *)crt;  // Pointer to the certificate data
unsigned char * key_DER = (unsigned char *)key;  // Pointer to the private key data
uint16_t crt_DER_len = sizeof(CRT);              // Length of the certificate data
uint16_t key_DER_len = sizeof(KEY);

// Create certificate data (see extras/README.md on how to create it)
SSLCert cert = SSLCert(
    crt_DER,      // DER-formatted certificate data
    crt_DER_len,  // length of the certificate data
    key_DER,      // private key for that certificate
    key_DER_len   // Length of the private key
);

// First, we create the HTTPSServer with the certificate created above
HTTPSServer secureServer = HTTPSServer(&cert);

// Additionally, we create an HTTPServer for unencrypted traffic
HTTPServer insecureServer = HTTPServer();

// Declare some handler functions for the various URLs on the server
void handleRoot(HTTPRequest * req, HTTPResponse * res);
void handle404(HTTPRequest * req, HTTPResponse * res);
void handleControl(HTTPRequest * req, HTTPResponse * res);
void handleControlSolve(HTTPRequest * req, HTTPResponse * res);
void handleRemoteControl(HTTPRequest * req, HTTPResponse * res);
void handleRemoteControlSolve(HTTPRequest * req, HTTPResponse * res);
void handleImu(HTTPRequest * req, HTTPResponse * res);
void handleImuSolve(HTTPRequest * req, HTTPResponse * res);

void setupServer()
{
    // 创建wifi热点
    WiFi.softAP(ssid, password);
    Serial.println("热点已创建");
    Serial.print("IP address: ");
    Serial.println(WiFi.softAPIP());

    // For every resource available on the server, we need to create a ResourceNode
    // The ResourceNode links URL and HTTP method to a handler function
    ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot);
    ResourceNode * node404 = new ResourceNode("", "GET", &handle404);
    ResourceNode * nodeControl = new ResourceNode("/control", "GET", &handleControl);
    ResourceNode * nodeControlSolve =
        new ResourceNode("/control_solve", "GET", &handleControlSolve);
    ResourceNode * nodeRemoteControl = new ResourceNode("/rc", "GET", &handleRemoteControl);
    ResourceNode * nodeRemoteControlSolve =
        new ResourceNode("/rc_solve", "GET", &handleRemoteControlSolve);
    ResourceNode * nodeImu = new ResourceNode("/imu", "GET", &handleImu);
    ResourceNode * nodeImuSolve = new ResourceNode("/imu_solve", "GET", &handleImuSolve);

    // Add the root node to the servers. We can use the same ResourceNode on multiple
    // servers (you could also run multiple HTTPS servers)
    secureServer.registerNode(nodeRoot);
    insecureServer.registerNode(nodeRoot);

    // We do the same for the default Node
    secureServer.setDefaultNode(node404);
    insecureServer.setDefaultNode(node404);

    // set the control node
    secureServer.registerNode(nodeControl);
    insecureServer.registerNode(nodeControl);

    // set the control solve node
    secureServer.registerNode(nodeControlSolve);
    insecureServer.registerNode(nodeControlSolve);

    // set the remote control node
    secureServer.registerNode(nodeRemoteControl);
    insecureServer.registerNode(nodeRemoteControl);

    // set the remote control solve node
    secureServer.registerNode(nodeRemoteControlSolve);
    insecureServer.registerNode(nodeRemoteControlSolve);

    // set the imu node
    secureServer.registerNode(nodeImu);
    insecureServer.registerNode(nodeImu);

    // set the imu solve node
    secureServer.registerNode(nodeImuSolve);
    insecureServer.registerNode(nodeImuSolve);

    // start the servers
    Serial.println("Starting HTTPS server...");
    secureServer.start();
    Serial.println("Starting HTTP server...");
    insecureServer.start();
    if (secureServer.isRunning() && insecureServer.isRunning()) {
        Serial.println("Servers ready.");
    }
}

void loopServer()
{
    // We need to call both loop functions here
    secureServer.loop();
    insecureServer.loop();
}

/*******************************************************************************/
/* 页面服务                                                                     */
/*******************************************************************************/

void handleRoot(HTTPRequest * req, HTTPResponse * res)
{
    res->setHeader("Content-Type", "text/html");

    res->println("<!DOCTYPE html>");
    res->println("<html>");
    res->println("<head><title>Hello World!</title></head>");
    res->println("<body>");
    res->println("<h1>Hello World!</h1>");

    res->print("<p>Your server is running for ");
    res->print((int)(millis() / 1000), DEC);
    res->println(" seconds.</p>");

    // You can check if you are connected over a secure connection, eg. if you
    // want to use authentication and redirect the user to a secure connection
    // for that
    if (req->isSecure()) {
        res->println("<p>You are connected via <strong>HTTPS</strong>.</p>");
    } else {
        res->println("<p>You are connected via <strong>HTTP</strong>.</p>");
    }

    res->println("</body>");
    res->println("</html>");
}

void handle404(HTTPRequest * req, HTTPResponse * res)
{
    req->discardRequestBody();
    res->setStatusCode(404);
    res->setStatusText("Not Found");
    res->setHeader("Content-Type", "text/html");
    res->println("<!DOCTYPE html>");
    res->println("<html>");
    res->println("<head><title>Not Found</title></head>");
    res->println(
        "<body><h1>404 Not Found</h1><p>The requested resource was not found on this "
        "server.</p></body>");
    res->println("</html>");
}

/**
 * @brief 控制页面
 * @note 通过 (192.168.4.1/control) 可以访问到这个页面
 */
void handleControl(HTTPRequest * req, HTTPResponse * res)
{
    res->setHeader("Content-Type", "text/html");
    res->println(CONTROL_HTML);
}

/**
 * @brief 控制页面处理
 * @note 通过 (192.168.4.1/control_solve) 可以访问到这个页面
 */
void handleControlSolve(HTTPRequest * req, HTTPResponse * res)
{
    ResourceParameters * Parameter = req->getParams();
    std::string ctrl_code;
    Parameter->getQueryParameter("ctrl_code", ctrl_code);

    Serial.print("ctrl_code: ");
    Serial.println(String(ctrl_code.c_str()));

    if (ctrl_code == "forward") {
        resetRCData();
        updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(3, 100);
    } else if (ctrl_code == "left") {
        resetRCData();
        updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(0, -100);
    } else if (ctrl_code == "right") {
        resetRCData();
        updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(0, 100);
    } else if (ctrl_code == "backward") {
        resetRCData();
        updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(3, -100);
    } else if (ctrl_code == "stop") {
        resetRCData();
        updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
    } else if (ctrl_code == "on") {
        resetRCData();
        communicateOn();
    } else if (ctrl_code == "off") {
        resetRCData();
        communicateOff();
    } else if (ctrl_code == "more") {
        resetRCData();
        updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(1, 100);
    } else if (ctrl_code == "less") {
        resetRCData();
        updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
        updateRCData(1, -100);
    } else {
        Serial.println("Unknown command");
    }
}

/**
 * @brief 遥控器页面
 * @note 通过 (192.168.4.1/rc) 可以访问到这个页面
 */
void handleRemoteControl(HTTPRequest * req, HTTPResponse * res)
{
    res->setHeader("Content-Type", "text/html");
    res->println(REMOTE_CONTROL_HTML);
}

/**
 * @brief 遥控器处理页面
 * @note 通过 (192.168.4.1/rc_solve) 可以访问到这个页面
 */
void handleRemoteControlSolve(HTTPRequest * req, HTTPResponse * res)
{
    ResourceParameters * Parameter = req->getParams();
    std::string ctrl_code_string;
    Parameter->getQueryParameter("ctrl_code", ctrl_code_string);

    String ctrl_code = String(ctrl_code_string.c_str());
    Serial.print("ctrl_code: ");
    Serial.println(ctrl_code);

    // 解析字符串，获取 id 的值
    int id_start = ctrl_code.indexOf("id=") + 3;
    int id_end = ctrl_code.indexOf(",", id_start);
    String id = ctrl_code.substring(id_start, id_end);

    if (id == "joystick-1" || id == "joystick-2") {
        // 解析字符串，获取 x 和 y 的值
        int x_start = ctrl_code.indexOf("x=") + 2;
        int x_end = ctrl_code.indexOf(",", x_start);
        String x = ctrl_code.substring(x_start, x_end);
        int y_start = ctrl_code.indexOf("y=") + 2;
        int y_end = ctrl_code.indexOf(",", y_start);
        String y = ctrl_code.substring(y_start, y_end);
        Serial.print("ID: ");
        Serial.println(id);
        Serial.print("X: ");
        Serial.println(x);
        Serial.print("Y: ");
        Serial.println(y);

        switch (rc_type) {
            case RC_DT7: {
                if (id == "joystick-1") {
                    updateRCData(2, 660 * x.toFloat());
                    updateRCData(3, -660 * y.toFloat());
                } else if (id == "joystick-2") {
                    updateRCData(0, 660 * x.toFloat());
                    updateRCData(1, -660 * y.toFloat());
                }
            } break;

            case RC_ET08A: {
                if (id == "joystick-1") {
                    updateRCData(2, 0.3 * 670 * x.toFloat());
                    updateRCData(3, 0.3 * 670 * y.toFloat());
                } else if (id == "joystick-2") {
                    updateRCData(0, 0.3 * 670 * x.toFloat());
                    updateRCData(1, -0.3 * 670 * y.toFloat());
                }
            } break;

            default:
                break;
        }

    } else if (id == "slider1" || id == "slider2") {
        Serial.print("ID: ");
        Serial.println(id);
        int value_start = ctrl_code.indexOf("value=") + 6;
        int value_end = ctrl_code.indexOf(",", value_start);
        String value = ctrl_code.substring(value_start, value_end);
        Serial.print("Value: ");
        Serial.println(value);

        switch (rc_type) {
            case RC_DT7: {
                int map[3] = {RC_SW_DOWN, RC_SW_MID, RC_SW_UP};
                if (id == "slider1") {
                    updateRCData(6, map[value.toInt() + 1]);
                } else if (id == "slider2") {
                    updateRCData(5, map[value.toInt() + 1]);
                }
            } break;

            case RC_ET08A: {
                if (id == "slider1") {
                    updateRCData(6, ET08A_RC_CH_VALUE_OFFSET + value.toInt() * 670);
                } else if (id == "slider2") {
                    updateRCData(5, ET08A_RC_CH_VALUE_OFFSET + value.toInt() * 670);
                }
            } break;

            default:
                break;
        }

    } else if (id == "button_on" || id == "button_off") {
        int value_start = ctrl_code.indexOf("value=") + 6;
        int value_end = ctrl_code.indexOf(",", value_start);
        String value = ctrl_code.substring(value_start, value_end);
        Serial.print("Value: ");
        Serial.println(value);

        if (value == "on") {
            communicateOn();
        } else if (value == "off") {
            communicateOff();
        }

    } else {
        Serial.println("ID: unknown");
    }
}

/**
 * @brief IMU控制页面
 * @note 通过 (192.168.4.1/imu) 可以访问到这个页面
 */
void handleImu(HTTPRequest * req, HTTPResponse * res)
{
    res->setHeader("Content-Type", "text/html");
    res->println(IMU_HTML);
}

void handleImuSolve(HTTPRequest * req, HTTPResponse * res)
{
    ResourceParameters * Parameter = req->getParams();
    std::string ctrl_code_string;
    Parameter->getQueryParameter("ctrl_code", ctrl_code_string);

    String ctrl_code = String(ctrl_code_string.c_str());
    Serial.print("ctrl_code: ");
    Serial.println(ctrl_code);
}

/*******************************************************************************/
/* HTTP                                                                        */
/*******************************************************************************/

/**
 * @brief 根页面
 * @note 通过 esp32的ip(192.168.4.1) 可以访问到这个页面
 */
void handleRootPage()
{
    String html = ROOT_HTML;
    // web_server.send(200, "text/html", html);
}

/**
 * @brief 控制页面
 * @note 通过 (192.168.4.1/control) 可以访问到这个页面
 */
void handleControlPage()
{
    String html = CONTROL_HTML;
    // web_server.send(200, "text/html", html);
}

/**
 * @brief 控制页面处理
 * @note 通过 (192.168.4.1/control_solve) 可以访问到这个页面
 */
void handleControlSolvePage()
{
    // String ctrl_code = web_server.arg("ctrl_code");
    // Serial.println(ctrl_code);

    // if (ctrl_code == "forward") {
    //     resetRCData();
    //     updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(3, 100);
    // } else if (ctrl_code == "left") {
    //     resetRCData();
    //     updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(0, -100);
    // } else if (ctrl_code == "right") {
    //     resetRCData();
    //     updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(0, 100);
    // } else if (ctrl_code == "backward") {
    //     resetRCData();
    //     updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(3, -100);
    // } else if (ctrl_code == "stop") {
    //     resetRCData();
    //     updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
    // } else if (ctrl_code == "on") {
    //     resetRCData();
    //     communicateOn();
    // } else if (ctrl_code == "off") {
    //     resetRCData();
    //     communicateOff();
    // } else if (ctrl_code == "more") {
    //     resetRCData();
    //     updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(1, 100);
    // } else if (ctrl_code == "less") {
    //     resetRCData();
    //     updateRCData(5, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(6, ET08A_RC_CH_VALUE_OFFSET);
    //     updateRCData(1, -100);
    // } else {
    //     Serial.println("Unknown command");
    // }
}

/**
 * @brief 遥控器页面
 * @note 通过 (192.168.4.1/rc) 可以访问到这个页面
 */
void handleRemoteControl()
{
    String html = REMOTE_CONTROL_HTML;
    // web_server.send(200, "text/html", html);
}

/**
 * @brief 遥控器处理页面
 * @note 通过 (192.168.4.1/rc_solve) 可以访问到这个页面
 */
void handleRemoteControlSolve()
{
    // String ctrl_code = web_server.arg("ctrl_code");
    // Serial.println(ctrl_code);

    // // 解析字符串，获取 id 的值
    // int id_start = ctrl_code.indexOf("id=") + 3;
    // int id_end = ctrl_code.indexOf(",", id_start);
    // String id = ctrl_code.substring(id_start, id_end);

    // if (id == "joystick-1" || id == "joystick-2") {
    //     // 解析字符串，获取 x 和 y 的值
    //     int x_start = ctrl_code.indexOf("x=") + 2;
    //     int x_end = ctrl_code.indexOf(",", x_start);
    //     String x = ctrl_code.substring(x_start, x_end);
    //     int y_start = ctrl_code.indexOf("y=") + 2;
    //     int y_end = ctrl_code.indexOf(",", y_start);
    //     String y = ctrl_code.substring(y_start, y_end);
    //     Serial.print("ID: ");
    //     Serial.println(id);
    //     Serial.print("X: ");
    //     Serial.println(x);
    //     Serial.print("Y: ");
    //     Serial.println(y);

    //     switch (rc_type) {
    //         case RC_DT7: {
    //             if (id == "joystick-1") {
    //                 updateRCData(2, 660 * x.toFloat());
    //                 updateRCData(3, -660 * y.toFloat());
    //             } else if (id == "joystick-2") {
    //                 updateRCData(0, 660 * x.toFloat());
    //                 updateRCData(1, -660 * y.toFloat());
    //             }
    //         } break;

    //         case RC_ET08A: {
    //             if (id == "joystick-1") {
    //                 updateRCData(2, 0.3 * 670 * x.toFloat());
    //                 updateRCData(3, 0.3 * 670 * y.toFloat());
    //             } else if (id == "joystick-2") {
    //                 updateRCData(0, 0.3 * 670 * x.toFloat());
    //                 updateRCData(1, -0.3 * 670 * y.toFloat());
    //             }
    //         } break;

    //         default:
    //             break;
    //     }

    // } else if (id == "slider1" || id == "slider2") {
    //     Serial.print("ID: ");
    //     Serial.println(id);
    //     int value_start = ctrl_code.indexOf("value=") + 6;
    //     int value_end = ctrl_code.indexOf(",", value_start);
    //     String value = ctrl_code.substring(value_start, value_end);
    //     Serial.print("Value: ");
    //     Serial.println(value);

    //     switch (rc_type) {
    //         case RC_DT7: {
    //             int map[3] = {RC_SW_DOWN, RC_SW_MID, RC_SW_UP};
    //             if (id == "slider1") {
    //                 updateRCData(6, map[value.toInt() + 1]);
    //             } else if (id == "slider2") {
    //                 updateRCData(5, map[value.toInt() + 1]);
    //             }
    //         } break;

    //         case RC_ET08A: {
    //             if (id == "slider1") {
    //                 updateRCData(6, ET08A_RC_CH_VALUE_OFFSET + value.toInt() * 670);
    //             } else if (id == "slider2") {
    //                 updateRCData(5, ET08A_RC_CH_VALUE_OFFSET + value.toInt() * 670);
    //             }
    //         } break;

    //         default:
    //             break;
    //     }

    // } else if (id == "button_on" || id == "button_off") {
    //     int value_start = ctrl_code.indexOf("value=") + 6;
    //     int value_end = ctrl_code.indexOf(",", value_start);
    //     String value = ctrl_code.substring(value_start, value_end);
    //     Serial.print("Value: ");
    //     Serial.println(value);

    //     if (value == "on") {
    //         communicateOn();
    //     } else if (value == "off") {
    //         communicateOff();
    //     }

    // } else {
    //     Serial.println("ID: unknown");
    // }
}

/**
 * @brief IMU控制页面
 * @note 通过 (192.168.4.1/imu) 可以访问到这个页面
 */
void handleImu()
{
    String html = IMU_HTML;
    // web_server.send(200, "text/html", html);
}

/**
 * @brief IMU控制页面处理
 * @note 通过 (192.168.4.1/imu_solve) 可以访问到这个页面
 */
void handleImuSolve()
{
    // String ctrl_code = web_server.arg("ctrl_code");
    // Serial.println(ctrl_code);
}

/**
 * @brief 用于跳转页面
 */
void handleTurnPage()
{
    // String page = web_server.arg("page");
    // if (page == "control") {
    // handleControlPage();
    // }
}

// void creatWebServer()
// {
//     // 创建wifi热点
//     WiFi.softAP(ssid, password);
//     Serial.println("热点已创建");
//     Serial.print("IP address: ");
//     Serial.println(WiFi.softAPIP());

//     Serial.println("HTTP server started");
// }

// void handleClient()
// {  //web_server.handleClient();
// }
}  // namespace mobile_remote_control_server
// END =====================================================
